package com.example.myweather;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.myweather.entity.City;
import com.example.myweather.entity.Result;
import com.example.myweather.service.WeatherManager;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Weatherfragment extends Fragment {
  public TextView city1;
  public TextView date;
  public TextView temp;
  public TextView type;
  public ImageView imageView;
  public TextView time;
  public ListView listView;
  public  Button button;

  String mCityName ;

    public Weatherfragment() {
        // Required empty public constructor

    }

    public void setSelectCity(String cityName){
        this.mCityName =cityName;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_weathergment, container, false);
         city1 = view.findViewById(R.id.cityName);
         date = view.findViewById(R.id.TodayDate);
         temp = view.findViewById(R.id.wendu);
         type = view.findViewById(R.id.typeId);
         time = view.findViewById(R.id.timeId);
          imageView= view.findViewById(R.id.imageView);
          listView=view.findViewById(R.id.liebiao);
          button=view.findViewById(R.id.button);



          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  FragmentManager fragmentManager = getFragmentManager();
                  fragmentManager.beginTransaction()
                          .replace(R.id.weather,new CityFragment())
                          .addToBackStack(null)
                          .commit();
              }
          });
         Button button3 = view.findViewById(R.id.button3);
         button3.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                Intent intent = getActivity().getIntent();
                String a = mCityName;//intent.getStringExtra("city");
                ReadData readData = new ReadData();
                readData.execute(a);
                city1.setText(CityFragment.city.getResult().get(0).getCitynm());
                date.setText(CityFragment.city.getResult().get(1).getDays());
                temp.setText(CityFragment.city.getResult().get(2).getTemperature());
                 type.setText(CityFragment.city.getResult().get(3).getWeather());
                 time.setText(CityFragment.city.getResult().get(4).getWeek());
                 Picasso.with(getContext()).load(CityFragment.city.getResult().get(0).getWeather_icon()).into(imageView);

             }

         });

        return  view;


    }
    class ReadData extends AsyncTask<String,Void,City>{

        @Override
        protected City doInBackground(String... strings) {
            WeatherManager manager = new WeatherManager();

            try {
                return manager.getCitynm(strings[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(City city) {
            CondAapter  condAapter =new CondAapter(city.getResult());
            ListView listView=getView().findViewById(R.id.liebiao);
            listView.setAdapter(condAapter);
            super.onPostExecute(city);
        }
    }
    class  CondAapter extends BaseAdapter{
     private  List<Result> mCities;

        public CondAapter(List<Result> mCities) {
            this.mCities = mCities;
        }

        @Override
        public int getCount() {

            return mCities.size();

        }

        @Override
        public Object getItem(int position) {
            return mCities.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view1=View.inflate(getContext(),R.layout.listview,null);
            Result result=mCities.get(position);
            TextView textView1=view1.findViewById(R.id.textView5);
            TextView textView2=view1.findViewById(R.id.textView6);
            TextView textView3=view1.findViewById(R.id.textView7);
            ImageView imageView2=view1.findViewById(R.id.imageView2);


            textView1.setText(result.getDays());
            textView2.setText(result.getWeather());
            textView3.setText(result.getTemperature());
            Picasso.with(getContext()).load(CityFragment.city.getResult().get(2).getWeather_icon()).into(imageView2);

            return view1;
        }
    }



}

